package com.jv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jv.entity.Product;
import com.jv.entity.ProductSwiperImage;
import com.jv.entity.R;
import com.jv.service.IProductService;
import com.jv.service.IProductSwiperImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 商品Controller
 * @author Jv
 * @version 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductSwiperImageService iProductSwiperImageService;

    @GetMapping("/findSwiper")//查询轮播商品
    public R findSwiper(){
        List<Product> swiperProductList = productService.list(new QueryWrapper<Product>()
                .eq("isSwiper", true).orderByAsc("swiperSort"));
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",swiperProductList);
        return R.ok(map);
    }

    //查询热门商品8个
    @GetMapping("/findHot")
    public R findHot(){
        Page<Product> page = new Page<>(0,8);
        Page<Product> productPage = productService.page(page, new QueryWrapper<Product>()
                .eq("isHot", true).orderByDesc("hotDateTime"));
        List<Product> hotProductList = productPage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",hotProductList);
        return R.ok(map);
    }

    //根据id查询商品信息
    @GetMapping("/detail")
    public R detail(Integer id){
        Product product = productService.getById(id);
        List<ProductSwiperImage> productSwiperImageList = iProductSwiperImageService.list(new QueryWrapper<ProductSwiperImage>()
                .eq("productId", product.getId()).orderByAsc("sort"));
        product.setProductSwiperImageList(productSwiperImageList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",product);
        return R.ok(map);
    }

    //商品搜索
    @RequestMapping("/search")
    public R search(String q){
        List<Product> productList = productService.list(new QueryWrapper<Product>().like("name", q));
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",productList);
        return R.ok(map);
    }
}
